package common;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import db.DBManager;

public class Category 
{
	private String code;
	private String name;
	
	/**
	 * カテゴリテーブルのコードと名前を全件返す
	 * @return カテゴリ全件配列
	 */
	public static List<Category> category()
	{
		Connection con = DBManager.getConnection();
		List<Category> list = new ArrayList<Category>();
		try
		{
			String sql = "select * from category" ;
			Statement smt = con.createStatement();
			ResultSet rs = smt.executeQuery(sql);
			while(rs.next())
			{
				Category c = new Category();
				c.setCode(rs.getString("code"));
				c.setName(rs.getString("name"));
				list.add(c);
			}
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return list;
	}
	
	public static String find_by_code(String code)
	{
		Connection con = DBManager.getConnection();
		String cName = null;
		try
		{
			String sql = "select name from category where code = " + code ;
			Statement smt = con.createStatement();
			ResultSet rs = smt.executeQuery(sql);
			if(rs.next())
			{
				cName = (rs.getString("name"));
			}
			rs.close();
			smt.close();
			con.close();
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		return cName;
	}
	
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}
